package cn.stylefeng.guns.modular.comm.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.comm.entity.MoneyHistory;
import cn.stylefeng.guns.modular.comm.mapper.MoneyHistoryMapper;
import cn.stylefeng.guns.modular.comm.model.params.MoneyHistoryParam;
import cn.stylefeng.guns.modular.comm.model.result.MoneyHistoryResult;
import  cn.stylefeng.guns.modular.comm.service.MoneyHistoryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 资金历史 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-05-10
 */
@Service
public class MoneyHistoryServiceImpl extends ServiceImpl<MoneyHistoryMapper, MoneyHistory> implements MoneyHistoryService {

    @Override
    public void add(MoneyHistoryParam param){
        MoneyHistory entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MoneyHistoryParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MoneyHistoryParam param){
        MoneyHistory oldEntity = getOldEntity(param);
        MoneyHistory newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MoneyHistoryResult findBySpec(MoneyHistoryParam param){
        return null;
    }

    @Override
    public List<MoneyHistoryResult> findListBySpec(MoneyHistoryParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MoneyHistoryParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MoneyHistoryParam param){
        return param.getSeqId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MoneyHistory getOldEntity(MoneyHistoryParam param) {
        return this.getById(getKey(param));
    }

    private MoneyHistory getEntity(MoneyHistoryParam param) {
        MoneyHistory entity = new MoneyHistory();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
